package edu.pdx.LAMA.AndroidCourse.scheduler;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

import edu.pdx.LAMA.AndroidCourse.ModelLayer.EventDetails;
import edu.pdx.LAMA.AndroidCourse.ModelLayer.EventService;
//import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class AppDayViewFragment extends Fragment {

	private static final String TAG = "AppDayViewFragment";
	public static final String EXTRA_SELECTED_DATE = "edu.pdx.LAMA.AndroidCourse.scheduler.day.extraSelectedDate";
	private ArrayList<EventDetails> eventDetails;
	//private ArrayList<EventDetails> dailyEventDetails;
	private TextView textView;
	private ListView listView;
	private String selectedDate;
	private ImageButton leftButton;
	private ImageButton rightButton;
	private EventDetailsAdapter adapter;
	
	private EventService eventService;
	
	// Selected date related variables
	int day_of_date;
	int month_of_date;
	int year_of_date;

	OnDateChangeListenerFromDayView dateChangeListenerFromDayView; 
	public interface OnDateChangeListenerFromDayView{
		public void onDateClickListenerCallBackFromDayView(String date);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		eventService = new EventService(getActivity());
		
		//eventDetails = eventService.getDailyEvent();
		//onDateChangeListenerFromDayView
		//ArrayAdapter<EventDetails> adapter = new ArrayAdapter<EventDetails>(getActivity(), android.R.layout.simple_list_item_1, eventDetails);
		
		
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_appday, container, false);
		/////////////////////////////////////////////////
		selectedDate = getArguments().getString(AppDayViewFragment.EXTRA_SELECTED_DATE);
		Log.d(TAG, "On CreateView: selected date in day view fragment: "+selectedDate);
		eventDetails = (ArrayList<EventDetails>) eventService.getDailyEvents();
		
		ArrayList<EventDetails> temp = (ArrayList<EventDetails>) eventService.getDatewiseEvents(selectedDate);
		Iterator<EventDetails> it = temp.iterator();
		while(it.hasNext()){
			eventDetails.add(it.next());
		}
		
		/////////////////////////////////////////////////
		listView = (ListView) v.findViewById(R.id.list);
		adapter = new EventDetailsAdapter(eventDetails);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				EventDetails details = eventDetails.get(position);
				//EventDetails details = ((EventDetailsAdapter)getAdapter()).getItem(position);
				Intent i = new Intent(getActivity(), EventPagerActivity.class);
				//Log.d(TAG, "the position selected is: "+position);
				i.putExtra(EventDetailsFragment.EXTRA_EVENT_SELECTED, position);
				i.putExtra(EventDetailsFragment.EXTRA_EVENT_DATE_SELECTED, selectedDate);
				startActivity(i);
				Log.d(TAG, details.getTitle()+" was clicked..");
			}
			
		});
		textView = (TextView) v.findViewById(R.id.dateTextBox);
		Log.d(TAG, "Selected date in fragment right before setting it to text box "+selectedDate);
		String[] a = selectedDate.split("/");		
		int d = Integer.parseInt(a[0]);
		int m = Integer.parseInt(a[1]);
		int y = Integer.parseInt(a[2]);
		textView.setText(d+"/"+(m+1)+"/"+y);
		
		// The following section of the code handles the date movement
		
		
		leftButton = (ImageButton) v.findViewById(R.id.prevButton);
		
		
		leftButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String[] parts = selectedDate.split("/");
				day_of_date = Integer.parseInt(parts[0]);
				month_of_date = Integer.parseInt(parts[1]);
				year_of_date = Integer.parseInt(parts[2]);
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.DAY_OF_MONTH, day_of_date);
				calendar.set(Calendar.MONTH, month_of_date);
				calendar.set(Calendar.YEAR, year_of_date);
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				int getMonth = calendar.get(Calendar.MONTH)+1;
				dateChangeListenerFromDayView.onDateClickListenerCallBackFromDayView(calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR));
				textView.setText(calendar.get(Calendar.DAY_OF_MONTH)+"/"+getMonth+"/"+calendar.get(Calendar.YEAR));
				selectedDate=calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR);
				//////////////////////////////
				eventDetails = (ArrayList<EventDetails>) eventService.getDailyEvents();
				ArrayList<EventDetails> temp = (ArrayList<EventDetails>) eventService.getDatewiseEvents(selectedDate);
				Iterator<EventDetails> it1 = temp.iterator();
				while(it1.hasNext()){
					eventDetails.add(it1.next());
				}
				//adapter.notifyDataSetChanged();
				adapter = null;
				adapter = new EventDetailsAdapter(eventDetails);
				listView.setAdapter(adapter);
			}
		});
		
		rightButton = (ImageButton) v.findViewById(R.id.nextButton);
		rightButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String[] parts = selectedDate.split("/");
				day_of_date = Integer.parseInt(parts[0]);
				month_of_date = Integer.parseInt(parts[1]);
				year_of_date = Integer.parseInt(parts[2]);
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.DAY_OF_MONTH, day_of_date);
				calendar.set(Calendar.MONTH, month_of_date);
				calendar.set(Calendar.YEAR, year_of_date);
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				int getMonth = calendar.get(Calendar.MONTH)+1;
				dateChangeListenerFromDayView.onDateClickListenerCallBackFromDayView(calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR));
				textView.setText(calendar.get(Calendar.DAY_OF_MONTH)+"/"+getMonth+"/"+calendar.get(Calendar.YEAR));
				selectedDate=calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR);
				///////////////////////////////
				eventDetails = (ArrayList<EventDetails>) eventService.getDailyEvents();
				ArrayList<EventDetails> temp = (ArrayList<EventDetails>) eventService.getDatewiseEvents(selectedDate);
				Iterator<EventDetails> it2 = temp.iterator();
				while(it2.hasNext()){
					eventDetails.add(it2.next());
				}
				//adapter.notifyDataSetChanged();
				adapter = null;
				adapter = new EventDetailsAdapter(eventDetails);
				listView.setAdapter(adapter);
			}
		});
		//return super.onCreateView(inflater, container, savedInstanceState);
		Log.d(TAG, "Rendering fragment view");
		return v;
	}

	/*@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		//EventDetails details = (EventDetails) (getListAdapter()).getItem(position);
		EventDetails details = ((EventDetailsAdapter)getListAdapter()).getItem(position);
		// Creating intent to be started
		Intent i = new Intent(getActivity(), EventPagerActivity.class);
		Log.d(TAG, "the position selected is: "+position);
		i.putExtra(EventDetailsFragment.EXTRA_EVENT_SELECTED, position);
		// starting the new activity
		startActivity(i);
		Log.d(TAG, details.getTitle()+" was clicked..");
	}*/

	private class EventDetailsAdapter extends ArrayAdapter<EventDetails> {

		public EventDetailsAdapter(ArrayList<EventDetails> events) {
			super(getActivity(), 0, events);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			//return super.getView(position, convertView, parent);
			if (convertView == null){
				convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_event, null);
			}

			EventDetails details = getItem(position);

			TextView colorTextView = (TextView) convertView.findViewById(R.id.colorTextView);
			if (details.getPriority().equalsIgnoreCase("High")){
				colorTextView.setBackgroundResource(R.color.color1);
			} else if (details.getPriority().equalsIgnoreCase("Medium")){
				colorTextView.setBackgroundResource(R.color.color2);
			} else if (details.getPriority().equalsIgnoreCase("Low")){
				colorTextView.setBackgroundResource(R.color.color3);
			}
			TextView titleTextView = (TextView) convertView.findViewById(R.id.eventTitle);
			titleTextView.setText(details.getTitle());
			TextView startTimeTextView = (TextView) convertView.findViewById(R.id.eventStartDateList);
			startTimeTextView.setText("Start Time: "+details.getStartTime());
			TextView endTimeTextView = (TextView) convertView.findViewById(R.id.eventEndTimeList);
			endTimeTextView.setText("End Time: "+details.getEndTime());
			return convertView;
		}

	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try{
			dateChangeListenerFromDayView = (OnDateChangeListenerFromDayView) activity;
		} catch(ClassCastException e){
			Log.d(TAG, "Class Cast exception while attaching with the activity");
		} catch(Exception e){
			Log.d(TAG, "Some other exception while attaching with the activity");
		}
	}
}